package screen.component
{
	import com.adobe.serialization.json.JSON;
	import com.greensock.TweenLite;
	import com.greensock.easing.Quart;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.text.TextField;
	
	import helper.Services;
	import helper.ValueObject;
	
	import screen.SacombankScreen;

	public class Mission extends UI
	{
		private const _MISSION_AVATAR_FOLDER:String="../assets/avatar/mission/";
		private const _MISSION_UNIT_WIDTH:Number = 195;
		private var _mission_loader:URLLoader;
		private var _mission_popup:MovieClip;
		private var _mission_group:MovieClip;
		private var _service:Services;
		public function Mission():void
		{
			init();
		}
		public function init():void{
			_mission_popup = ValueObject.S1_RESOURCE.getExportedAsset("missionPopup") as MovieClip;
			popupInit(_mission_popup);
			_mission_group = new MovieClip();
			_mission_popup.addChild(_mission_group);
			
			with(_mission_popup){
				_mission_group.x = maskMc.x;
				_mission_group.y = maskMc.y;
				_mission_group.mask = maskMc;
				sliderInit(_mission_group,3,1,_mission_group.x,_MISSION_UNIT_WIDTH);
				buttonImageInit(nextBtn);
				MovieClip(nextBtn).addEventListener(MouseEvent.CLICK,nextMissionClick);
				buttonImageInit(preBtn);
				MovieClip(preBtn).addEventListener(MouseEvent.CLICK,preMissionClick);
				buttonImageInit(closeBtn);
				MovieClip(closeBtn).addEventListener(MouseEvent.CLICK,hideMissionPopup)
			}
			
			_service = Services.getInstance();
			_mission_loader = new URLLoader();
			getMission();
		}
		private function getMission():void{
			_mission_loader.addEventListener(Event.COMPLETE,getMissionComplete);
			_mission_loader.addEventListener(IOErrorEvent.IO_ERROR,getMissionError);
			_mission_loader.load(_service.getMissionListRequest());
		}
		private function getMissionComplete(e:Event):void{
			_mission_loader.removeEventListener(Event.COMPLETE,getMissionComplete);
			_mission_loader.removeEventListener(IOErrorEvent.IO_ERROR,getMissionError);
			//trace(e.target.data);
			var arr:Array = JSON.decode(e.target.data);
			updateMission(arr);
		}
		private function getMissionError(e:IOErrorEvent):void{
			_mission_loader.removeEventListener(Event.COMPLETE,getMissionComplete);
			_mission_loader.removeEventListener(IOErrorEvent.IO_ERROR,getMissionError);
		}
		private function updateMission(arr:Array):void{
			var leng:Number = arr.length;
			
			for(var i:Number=0;i<leng;i++){
				//trace("leng",arr[i].description);
				var mission_unit:MovieClip = ValueObject.S1_RESOURCE.getExportedAsset("missionUnit") as MovieClip;
				TextField(mission_unit.nameTf).text = arr[i].mission;
				TextField(mission_unit.goldTf).text =arr[i].description;
				mission_unit.avatar.load(new URLRequest(_MISSION_AVATAR_FOLDER + arr[i].missionId +".png"));
				 buttonImageInit(mission_unit.doMissionBtn);
				 MovieClip(mission_unit.doMissionBtn).no = i +1;
				 MovieClip(mission_unit.doMissionBtn).addEventListener(MouseEvent.CLICK,doMissionClick)
				 mission_unit.x = _mission_group.numChildren*_MISSION_UNIT_WIDTH;
				 _mission_group.addChild(mission_unit);
			}
			updateSliderBtn(_mission_group);
		}
		private function nextMissionClick(e:MouseEvent):void{
			sliderNextClick(_mission_group)
		}
		private function preMissionClick(e:MouseEvent):void{
			sliderPreClick(_mission_group);
		}
		public function showMissionPopup():void{
			popupShow(_mission_popup);
		}
		public function hideMissionPopup(e:MouseEvent=null):void{
			popupHide(_mission_popup);
		}
		private function doMissionClick(e:MouseEvent):void{
			popupHide(_mission_popup);
			SacombankScreen.getInstance().sliderPlace(e);
		}
	}
}